package com.jqb.ajaxlab.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jqb.ajaxlab.pojo.User;


public class Ajax05 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*Json*/
		response.setHeader("pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		List<User> users = new ArrayList<User>();
		for(int i=1000;i<1005;i++){
			User user = new User();
			user.setId(i);
			user.setName("admin"+i);
			users.add(user);
		}
		Gson gson = new Gson();
		/*List转换成Json数据*/
		String json = gson.toJson(users);
		out.print(json);
	}
}
